<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/9 0009
 * Time: 16:28
 */
include_once 'class/simple_html_dom.php';
function __GetHtml($url)
{
    $cacheObj=new CacheHelper();
    $_value=$cacheObj->Get($url);
    if ($_value)
    {
        return $_value;
    }
    $html=http_request($url);
    $cacheObj->Set($url,$html);
    return $html;
}
/* 通用CURL请求
* @param $url  需要请求的url
* @param null $data
* return mixed 返回值 json格式的数据
*/
function http_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $info = curl_exec($curl);
    curl_close($curl);
    return $info;
}
class CacheHelper
{
    protected $dir = '';
    protected $flag=false;
    protected $mObj;

    public function __construct()
    {
        if (class_exists("Memcache"))
        {
            $this->mObj = new Memcache();
            $this->flag = $this->mObj->connect("127.0.0.1", 11211);
        }


        if (!$this->flag)
        {
            $this->dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cache';
            if (is_dir($this->dir))
            {
                return;
            }
            @mkdir($this->dir);
        }
    }



    public function Set($key, $value, $expire = 360)
    {
        if ($this->flag)
        {
            $this->mObj->set(md5($key), $value, 0, $expire);
        }
        else
        {
            $data = array(
                'time' => time(),
                'expire' => $expire,
                'value' => $value
            );
            @file_put_contents($this->dir . DIRECTORY_SEPARATOR . md5($key) . 'cache', serialize($data));
        }

    }

    public function Get($key)
    {
        if ($this->flag)
        {
            return $this->mObj->get(md5($key));
        }
        else
        {
            $file = $this->dir . DIRECTORY_SEPARATOR . md5($key) . 'cache';
            if (!file_exists($file))
            {
                return false;
            }
            $str = @file_get_contents($file);
            if (empty($str))
            {
                return false;
            }
            $data = @unserialize($str);
            if (!isset($data['time']) || !isset($data['expire']) || !isset($data['value']))
            {
                return false;
            }
            if ($data['time'] + $data['expire'] < time())
            {
                return false;
            }
            return $data['value'];
        }

    }

    public function clean()
    {

        if ($this->flag)
        {
            $this->mObj->flush();
        }else
        {
            if (!empty($this->dir) && is_dir($this->dir))
            {
                @rmdir($this->dir);
            }
            $files = scandir($this->dir);
            foreach ($files as $file)
            {
                @unlink($this->dir . DIRECTORY_SEPARATOR . $file);
            }
        }

    }
}